<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\UploadFile;
use App\Http\Requests\InsertBannerRequest;
use App\Http\Resources\BannerCollection;
use App\Models\Banner;
use App\Models\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BannerController extends Controller
{
    use UploadFile;

    /**
     * banner列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $banner = Banner::with('course:id,name')->get();
        return view('admin.banner.index', compact('banner'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $courses = Course::get(['id', 'name']);
        return view('admin.banner.create', compact('courses'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param InsertBannerRequest $request
     * @return array
     */
    public function store(InsertBannerRequest $request)
    {
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['url'] = '/uploads/' . $fileinfo->getFilename();
        } else {
            return ['status' => 1, 'msg' => '文件上传异常，请重试'];
        }
        $data['course_id'] = $request->get('course_id');

        $res = Banner::create($data);
        return $res ? ['status' => 0, 'msg' => '成功'] : ['status' => 1, 'msg' => '失败'];
//        if ($res) return ['status' => 0, 'msg' => '成功'];
//        return ['status' => 1, 'msg' => '失败'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $courses = Course::get(['id', 'name']);
        $banner = Banner::find($id, ['id', 'url', 'course_id']);
        return view('admin.banner.edit', compact('courses', 'banner'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param InsertBannerRequest $request
     * @param  int $id
     * @return array
     */
    public function update(InsertBannerRequest $request, $id)
    {
        if ($request->hasFile('file')) {
//            上传成功，调用上传方法
            $fileinfo = $this->up($request->file('file'));
            $fileinfo && $data['url'] = '/uploads/' . $fileinfo->getFilename();
        } else {
            return ['status' => 1, 'msg' => '文件上传异常，请重试'];
        }
        $data['course_id'] = $request->get('course_id');
        $res = Banner::where('id', $id)->update($data);
        if ($res) return ['status' => 0, 'msg' => '成功'];
        return ['status' => 1, 'msg' => '失败'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
